package com.mobisys.serverInfo;

import com.mobisys.common.serverInfos.AbstractServerInfos;
import com.mobisys.common.serverInfos.LinuxServerInfos;
import com.mobisys.common.serverInfos.WindowsServerInfos;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

/**
 * @Author：qzxl
 * @Date：2020-12-17
 * @Description：license-service
 */
public class GenerateServerInfo {
    private final static String BASEPATH = System.getProperty("user.dir");
    private final static Logger logger = LoggerFactory.getLogger(GenerateServerInfo.class);


    /**
     * 将服务器硬件信息保存到文件中
     */
    private static void generateServerINfoToFile() {
        final String osName = System.getProperty("os.name").toLowerCase();
        AbstractServerInfos abstractServerInfos = null;
        //根据不同操作系统类型选择不同的数据获取方法
        if (osName.startsWith("windows")) {
            abstractServerInfos = new WindowsServerInfos();
        } else if (osName.startsWith("linux")) {
            abstractServerInfos = new LinuxServerInfos();
        } else {//其他服务器类型
            abstractServerInfos = new LinuxServerInfos();
        }
        FileWriter fileWriter = null;
        BufferedWriter bufferedWriter = null;
        System.out.println(abstractServerInfos.getServerInfos());
        try {
            fileWriter = new FileWriter(BASEPATH + File.separator + "server.txt");
            bufferedWriter = new BufferedWriter(fileWriter);
            bufferedWriter.write(abstractServerInfos.getServerInfos().getCpuSerial());
            bufferedWriter.write(abstractServerInfos.getServerInfos().getMainBoardSerial());
            bufferedWriter.flush();
            logger.info("生成服务器硬件信息成功！");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bufferedWriter != null) {
                    bufferedWriter.close();
                }
                if (fileWriter != null) {
                    fileWriter.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }


    public static void main(String args[]) {
        generateServerINfoToFile();
    }
}
